package com.skydroid.fpvplayerdemo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import org.jetbrains.annotations.Nullable;
/**
 * @author 咔一下
 * @date 2023/4/20 15:22
 * @email 1501020210@qq.com
 * @describe
 */
public class InputUrlDialogFragment extends DialogFragment {
    private static final String Key_PositiveButton_Text = "Key_PositiveButton_Text";
    private static final String Key_NegativeButton_Text = "Key_NegativeButton_Text";
    private static final String Key_DefUrl_Value = "Key_DefUrl_Value";

    private Runnable runnable;

    private ButtonOnClickListener positiveButtonListener;
    private ButtonOnClickListener negativeButtonListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        Runnable runnable = this.runnable;
        if (runnable != null){
            runnable.run();
        }
    }

    public void setOnDismissCallBack(Runnable runnable){
        this.runnable = runnable;
    }

    private String getStringArgument(String key){
        Bundle b = getArguments();
        if (b == null){
            return "";
        }
        String s = b.getString(key);
        if (s == null){
            return "";
        }
        return s;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_input_url, null);
        final EditText et_url = view.findViewById(R.id.et_url);
        et_url.setText(getStringArgument(Key_DefUrl_Value));
        String pb = getStringArgument(Key_PositiveButton_Text);
        if (TextUtils.isEmpty(pb)){
            pb = "确定";
        }
        String nb = getStringArgument(Key_NegativeButton_Text);
        if (TextUtils.isEmpty(nb)){
            nb = "取消";
        }

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(view)
                // Add action buttons
                .setPositiveButton(pb, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        String url = et_url.getText().toString().trim();
                        ButtonOnClickListener l = positiveButtonListener;
                        if (l != null){
                            l.onClick(dialog, id,url);
                        }
                        InputUrlDialogFragment.this.getDialog().cancel();
                    }
                })
                .setNegativeButton(nb, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        ButtonOnClickListener l = negativeButtonListener;
                        if (l != null){
                            l.onClick(dialog, id,"");
                        }
                        InputUrlDialogFragment.this.getDialog().cancel();
                    }
                });
        return builder.create();
    }

    public static InputUrlDialogFragment createDialog(String positiveButton,
                                                             ButtonOnClickListener positiveButtonListener,
                                                             String negativeButton,
                                                             ButtonOnClickListener negativeButtonListener,
                                                             String defUrl){
        InputUrlDialogFragment dialog = new InputUrlDialogFragment();
        Bundle bundle = new Bundle();
        if (!TextUtils.isEmpty(positiveButton)){
            bundle.putString(Key_PositiveButton_Text,positiveButton);
        }
        if (!TextUtils.isEmpty(negativeButton)){
            bundle.putString(Key_NegativeButton_Text,negativeButton);
        }
        if (!TextUtils.isEmpty(defUrl)){
            bundle.putString(Key_DefUrl_Value,defUrl);
        }
        dialog.setArguments(bundle);
        dialog.positiveButtonListener = positiveButtonListener;
        dialog.negativeButtonListener = negativeButtonListener;
        return dialog;
    }

    public interface ButtonOnClickListener{
        public void onClick(DialogInterface dialog, int id,String s);
    }
}
